package com.jrx.anytxn.param.service.product.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.product.InstallmentFeeTableReq;
import com.jrx.anytxn.param.dto.product.InstallmentFeeTableRes;
import com.jrx.anytxn.param.entity.PrInstallmentFeeTable;
import com.jrx.anytxn.param.mapper.ext.ExtPrInstallmentFeeTableMapper;
import com.jrx.anytxn.param.service.product.IInstallmentFeeTableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 分期手续费表 业务接口实现
 *
 * @author yx
 * @date 2019/5/28
 */
@Service
@CacheConfig(cacheNames = "INSTALLMENT")
public class InstallmentFeeTableServiceImpl implements IInstallmentFeeTableService {
    private static final Logger logger = LoggerFactory.getLogger(InstallmentFeeTableServiceImpl.class);
    @Resource
    private ExtPrInstallmentFeeTableMapper extPrInstallmentFeeTableMapper;

    @Autowired
    private ISegmentService segmentService;

    @Override
    public TxnPage<InstallmentFeeTableRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<PrInstallmentFeeTable> page = PageHelper.startPage(pageNum, pageSize,Constant.ORDER_BY_SORT);
        List<PrInstallmentFeeTable> installmentFeeTableList = extPrInstallmentFeeTableMapper.selectByTenId(tenantId);
        if (installmentFeeTableList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<InstallmentFeeTableRes> res = BeanMapping.copyList(installmentFeeTableList, InstallmentFeeTableRes.class);
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), res);
    }

    @Override
    public InstallmentFeeTableRes addInstallmentFeeTable(InstallmentFeeTableReq installmentFeeTableReq) throws TxnException {
        if (installmentFeeTableReq.getMaxAmount() != null && installmentFeeTableReq.getMinAmount() != null && installmentFeeTableReq.getMaxAmount().compareTo(installmentFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInstallmentFeeTable prInstallmentFeeTable = extPrInstallmentFeeTableMapper.selectByInstallmentFeeId(installmentFeeTableReq.getInstallmentFeeId(), installmentFeeTableReq.getOrganizationId(),installmentFeeTableReq.getTenantId());
        if (prInstallmentFeeTable != null) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        PrInstallmentFeeTable installmentFeeTable = BeanMapping.copy(installmentFeeTableReq, PrInstallmentFeeTable.class);
        try {
            installmentFeeTable.setId(segmentService.getId(Constant.PR_INSTALLMENTFEE_TABLE));
        } catch (SegmentException e) {
            logger.error("主键生成错误",e);
            throw new TxnException(ErrorMsgConstant.ERROR_ID);
        }
        installmentFeeTable.setRecordVersionNumber(Constant.VERSION_NUMBER);
        extPrInstallmentFeeTableMapper.insertSelective(installmentFeeTable);
        return BeanMapping.copy(installmentFeeTable, InstallmentFeeTableRes.class);
    }

    @CacheEvict(key = "'installment_'+#installmentFeeTableReq.tenantId+'_'+#installmentFeeTableReq.installmentFeeId")
    @Override
    public InstallmentFeeTableRes modifyInstallmentFeeTable(InstallmentFeeTableReq installmentFeeTableReq) throws TxnException {
        if (installmentFeeTableReq.getMaxAmount() != null && installmentFeeTableReq.getMinAmount() != null && installmentFeeTableReq.getMaxAmount().compareTo(installmentFeeTableReq.getMinAmount()) < 0) {
            throw new TxnBizException(TxnRespCode.BIZ_ERROR.getCode(), "输入有误,最大费用值要比最小费用值大");
        }
        PrInstallmentFeeTable prInstallmentFeeTable = extPrInstallmentFeeTableMapper.selectByInstallmentFeeIdAndTenId(installmentFeeTableReq.getInstallmentFeeId(), installmentFeeTableReq.getTenantId());
        if (prInstallmentFeeTable == null) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        PrInstallmentFeeTable installmentFeeTable = BeanMapping.copy(installmentFeeTableReq, PrInstallmentFeeTable.class);
        // 更新
        extPrInstallmentFeeTableMapper.updateByTenIdAndFeeId(installmentFeeTable);
        return BeanMapping.copy(installmentFeeTable, InstallmentFeeTableRes.class);

    }

    @CacheEvict(key = "'installment_'+#tenantId+'_'+#installmentFeeId")
    @Override
    public Boolean removeByTenIdAndFeeId(String tenantId, String installmentFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(installmentFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extPrInstallmentFeeTableMapper.deleteByTemIdAndFeeId(installmentFeeId, tenantId);
        return i > 0;
    }

    @Override
    public InstallmentFeeTableRes findByTenIdAndFeeId(String tenantId, String installmentFeeId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(installmentFeeId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        PrInstallmentFeeTable installmentTable = extPrInstallmentFeeTableMapper.selectByInstallmentFeeIdAndTenId(installmentFeeId, tenantId);
        return BeanMapping.copy(installmentTable, InstallmentFeeTableRes.class);
    }

    @Override
    public InstallmentFeeTableRes findByInstallmentFeeId(String installmentFeeId, String ordId, String tenantId) throws TxnBizException {
        PrInstallmentFeeTable installmentTable = extPrInstallmentFeeTableMapper.selectByInstallmentFeeId(installmentFeeId, ordId, tenantId);
        if (installmentTable == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), ErrorMsgConstant.NO_FEE_DATA);
        }
        return BeanMapping.copy(installmentTable, InstallmentFeeTableRes.class);
    }

    @Cacheable(key = "'installment_'+#tenantId+'_'+#installmentFeeId")
    @Override
    public   PrInstallmentFeeTable findInstallmentByFeeId(String installmentFeeId, String ordId, String tenantId) throws TxnBizException {
        PrInstallmentFeeTable installmentTable = extPrInstallmentFeeTableMapper.selectByInstallmentFeeId(installmentFeeId, ordId, tenantId);
        if (installmentTable == null) {
            throw new TxnBizException(TxnRespCode.NO_DATA.getCode(), ErrorMsgConstant.NO_FEE_DATA);
        }
        return installmentTable;
    }
}
